﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using System;
using System.Linq;
using System.Windows.Input;
using Microsoft.Expression.Extensibility;
using Microsoft.Expression.Framework;
//using Microsoft.Expression.Framework.AddIn;
using Microsoft.Expression.Framework.Commands;
using Microsoft.Expression.Framework.UserInterface;

using Colorful.Control;
using Colorful.Control.Services;
using Microsoft.Expression.Framework.Workspaces.Extension;

namespace Colorful.Design.AddIn
{
    //[AddInDescription("Colorful Expression - Get colors online", AddInCategory.Tool)]
    public class ColorCommunityAddIn : CommandTarget, IPackage
    {
//        private IApplicationService appService;

        private IWindowService windowService;
        private ICommandService commandService;
        private ICommandBarService commandBarService;

        private SearchPanel colorPanel;

        public void Initialize()
        {
            //this.appService = appService;
            this.colorPanel = new SearchPanel(new ColorfulViewModel(
                                                new AdobeKuler(),
                                                new AdobeClipboardFormatter(),
                                                new CfXamlClipboardFormatter()), false);

            commandService.AddTarget(this);

            if (windowService.PaletteRegistry["ColorPanel"] == null)
                windowService.RegisterPalette("ColorPanel", colorPanel, "Colorful");


            var pallete = windowService.PaletteRegistry["ColorPanel"];
            pallete.IsVisible = true;

            //
            //            var workspace = (Workspace)appService.WindowService.ActiveWorkspace;
            //            var palette = appService.WindowService.ActiveWorkspace.AddPalette("ColorPanel", workspace.BottomPaletteGroup, true);
            //
            //            palette.ShowCloseButton = true;
            //            palette.CanCollapse = false;
            //            palette.CanResize = true;  
            //            palette.CanClose = true;
            //            palette.IsFloating = false;            
            //            palette.Height = 400;
            //            palette.Width = 260;
            //            palette.SetIsSelectedAsynchronously(false);
            //
            //            workspace.BottomPaletteGroup.Palettes.First().SetIsSelectedAsynchronously(true);

            //            var showHideCommand = new ShowHidePanelCommand(palette);
            //            var showHideCommandBinding = new KeyBinding(showHideCommand, Key.F6, ModifierKeys.None);
            //            this.AddCommand("ShowHideColorPanel", showHideCommand);

//            var windowMenu = ((ICommandBarMenu)commandBarService.CommandBars[0].Items[6]);
//            windowMenu.Items.AddSeparator();
            //            windowMenu.Items.AddCheckBox("ShowHideColorPanel", "Color Community", showHideCommandBinding);
        }

        public void Load(IServices services)
        {
            windowService = services.GetService<IWindowService>();
            commandService = services.GetService<ICommandService>();
            commandBarService = services.GetService<ICommandBarService>();
            Initialize();
        }


        public void Unload()
        {

        }
    }
}
